package com.have.blog.xo.service.admin.impl;

import com.github.pagehelper.PageHelper;

import com.have.blog.base.enums.BlogPublishStatus;
import com.have.blog.base.enums.BlogStatus;
import com.have.blog.model.api.CommonPage;
import com.have.blog.model.mapper.TBlogMapper;
import com.have.blog.model.mapper.TBlogSortMapper;
import com.have.blog.model.models.TBlog;
import com.have.blog.model.models.TBlogExample;
import com.have.blog.model.models.TBlogSort;
import com.have.blog.model.models.TBlogSortExample;
import com.have.blog.utils.MyStringUtils;
import com.have.blog.utils.ResultUtils;
import com.have.blog.xo.constant.MessageConstant;
import com.have.blog.xo.service.admin.AdminBlogSortService;
import com.have.blog.xo.vo.AdminBlogSortVo;
import com.have.blog.xo.vo.AdminBlogVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
@SuppressWarnings("all")
public class AdminBlogSortServiceImpl implements AdminBlogSortService {
    @Autowired
    private TBlogSortMapper blogSortMapper;
    @Autowired
    private TBlogMapper blogMapper;


    @Override
    public String getList(AdminBlogSortVo blogSortVO) {
        PageHelper.startPage(blogSortVO.getCurrentPage(), blogSortVO.getPageSize());
        TBlogSortExample example = new TBlogSortExample();
        TBlogSortExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(blogSortVO.getKeyword()) && !StringUtils.isEmpty(blogSortVO.getKeyword().trim())) {
            criteria.andSortNameLike(blogSortVO.getKeyword());
        }
        if(!StringUtils.isEmpty(blogSortVO.getOrderByAscColumn())) {
            // 将驼峰转换成下划线
            String column = MyStringUtils.underLine(new StringBuffer(blogSortVO.getOrderByAscColumn())).toString();
            example.setOrderByClause(column + " asc");
        } else if (!StringUtils.isEmpty(blogSortVO.getOrderByDescColumn())) {
            // 将驼峰转换成下划线
            String column = MyStringUtils.underLine(new StringBuffer(blogSortVO.getOrderByDescColumn())).toString();
            example.setOrderByClause(column + " desc");
        } else {
            example.setOrderByClause("sort desc");
        }
        criteria.andStatusEqualTo(BlogStatus.ENABLE.getStatus());
        List<TBlogSort> list = blogSortMapper.selectByExample(example);

        return ResultUtils.successWithData(CommonPage.restPage(list));
    }

    @Override
    public String addBlogSort(AdminBlogSortVo blogSortVO) {
        TBlogSortExample example = new TBlogSortExample();
        example.createCriteria()
                .andSortNameEqualTo(blogSortVO.getSortName())
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus());
        List<TBlogSort> tBlogSorts = blogSortMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(tBlogSorts)) {
            return ResultUtils.errorWithMessage(MessageConstant.ENTITY_EXIST);
        }
        TBlogSort blogSort = new TBlogSort();
        blogSort.setUid(MyStringUtils.getUUID());
        blogSort.setSortName(blogSortVO.getSortName());
        blogSort.setContent(blogSortVO.getContent());
        blogSort.setSort(blogSortVO.getSort());
        blogSort.setStatus(BlogStatus.ENABLE.getStatus());
        blogSort.setUpdateTime(new Date());
        blogSort.setCreateTime(new Date());

        blogSortMapper.insert(blogSort);
        return ResultUtils.successWithMessage(MessageConstant.INSERT_SUCCESS);
    }

    @Override
    public String editBlogSort(AdminBlogSortVo blogSortVO) {
        TBlogSort blogSort = blogSortMapper.selectByPrimaryKey(blogSortVO.getUid());
        if (blogSort == null) {
            return ResultUtils.errorWithMessage(MessageConstant.ENTITY_NOT_EXIST);
        }
        blogSort.setStatus(BlogStatus.ENABLE.getStatus());
        blogSort.setSortName(blogSortVO.getSortName());
        blogSort.setContent(blogSortVO.getContent());
        blogSort.setUpdateTime(new Date());
        blogSortMapper.updateByPrimaryKeySelective(blogSort);
        // TODO 缓存相关操作

        return ResultUtils.successWithMessage(MessageConstant.UPDATE_SUCCESS);
    }

    @Override
    public String deleteBatchBlogSort(List<AdminBlogSortVo> blogSortVoList) {
        if (CollectionUtils.isEmpty(blogSortVoList)) {
            return ResultUtils.errorWithMessage(MessageConstant.PARAM_INCORRECT);
        }
        List<String> uidList = new ArrayList<>();
        blogSortVoList.forEach(item -> {
            uidList.add(item.getUid());
        });

        TBlogExample example = new TBlogExample();
        example.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                .andBlogSortUidIn(uidList);
        List<TBlog> tBlogs = blogMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(tBlogs)) {
            return ResultUtils.errorWithMessage(MessageConstant.BLOG_UNDER_THIS_SORT);
        }

        TBlogSort blogSort = new TBlogSort();
        blogSort.setUpdateTime(new Date());
        blogSort.setStatus(BlogStatus.DISABLE.getStatus());
        TBlogSortExample sortExample = new TBlogSortExample();
        sortExample.createCriteria()
                .andUidIn(uidList);
        int count = blogSortMapper.updateByExampleSelective(blogSort, sortExample);
        // TODO 缓存相关操作
        return ResultUtils.successWithMessage(MessageConstant.DELETE_SUCCESS);
    }

    @Override
    public String stickBlogSort(AdminBlogVo blogSortVO) {
        PageHelper.startPage(1, 1);
        TBlogSortExample sortExample = new TBlogSortExample();
        sortExample.setOrderByClause("sort desc");
        List<TBlogSort> tBlogSorts = blogSortMapper.selectByExample(sortExample);
        if (CollectionUtils.isEmpty(tBlogSorts)) {
            return ResultUtils.errorWithMessage(MessageConstant.PARAM_INCORRECT);
        }
        TBlogSort maxSort = tBlogSorts.get(0);
        if (maxSort.getUid().equals(blogSortVO.getUid())) {
            return ResultUtils.errorWithMessage(MessageConstant.THIS_SORT_IS_TOP);
        }
        TBlogSort blogSort = blogSortMapper.selectByPrimaryKey(blogSortVO.getUid());
        blogSort.setSort(maxSort.getSort() + 1);
        blogSort.setUpdateTime(new Date());
        blogSortMapper.updateByPrimaryKeySelective(blogSort);
        return ResultUtils.successWithMessage(MessageConstant.OPERATION_SUCCESS);
    }

    @Override
    public String blogSortByClickCount() {
        TBlogSortExample example = new TBlogSortExample();
        example.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus());
        example.setOrderByClause("click_count desc");
        List<TBlogSort> blogSortList = blogSortMapper.selectByExample(example);
        for (TBlogSort blogSort : blogSortList) {
            blogSort.setSort(blogSort.getClickCount());
            blogSort.setUpdateTime(new Date());
            blogSortMapper.updateByPrimaryKeySelective(blogSort);
        }
        return ResultUtils.successWithMessage(MessageConstant.OPERATION_SUCCESS);
    }

    @Override
    public String blogSortByCite() {
        // 定义Map, key:tagUid, value:引用量
        Map<String, Integer> map = new HashMap<>();
        TBlogSortExample example = new TBlogSortExample();
        example.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus());
        List<TBlogSort> blogSortList = blogSortMapper.selectByExample(example);
        TBlogExample blogExample = new TBlogExample();
        blogExample.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                .andIsPublishEqualTo(BlogPublishStatus.PUBLISH.getStatus());
        List<TBlog> blogList = blogMapper.selectByExample(blogExample);

        blogList.forEach(item -> {
            String blogSortUid = item.getBlogSortUid();
            map.put(blogSortUid, map.getOrDefault(blogSortUid, 0) + 1);
        });

        // TODO 批量更新修改
        for (TBlogSort blogSort : blogSortList) {
            blogSort.setSort(map.getOrDefault(blogSort.getUid(), 0));
            blogSort.setUpdateTime(new Date());
            blogSortMapper.updateByPrimaryKeySelective(blogSort);
        }
        return ResultUtils.successWithMessage(MessageConstant.OPERATION_SUCCESS);
    }
}
